import OpenAI from "openai";
import 'dotenv/config'

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, baseURL: "https://api.chatanywhere.tech/v1" });

export async function generateDescriptionWithAI(content: string) {
	const prompt = `请用简洁的中文总结以下文章的核心内容，生成一句不超过50字的描述：\n\n${content.slice(0, 1000)}`;

	try {
		const res = await client.chat.completions.create({
			model: "gpt-4.1-nano", 
			messages: [{ role: "user", content: prompt }],
			temperature: 0.7,
		});

		return res.choices[0].message.content?.trim() || "";
	} catch (err) {
		console.error("AI 生成 description 失败", err);
		return "请自定义描述";
	}
}
